<?php

/**
 * This is the model class for table "zy_agent_company".
 *
 * The followings are the available columns in table 'zy_agent_company':
 * @property integer $agent_id
 * @property integer $agent_number
 * @property integer $user_id
 * @property string $name
 * @property string $phone
 * @property string $email
 */
class AgentCompany extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AgentCompany the static model class
	 */
	public $hotong;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		//这个表只是公司代理 个人代理的表 不是公司下面的员工的代理表
		return 'zy_agent_company';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('agent_number, user_id,credit,agent_type', 'numerical', 'integerOnly'=>true),
			array('name, phone, email', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			//array('hetong','file'),
			array('agent_id, agent_number,agent_type,user_id, name, phone, email,credit', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'agent_id' => 'Agent',
			'agent_number' => Yii::t('base','Agent Number'),
			'user_id' => 'User',
			'name' => Yii::t('base','User Name'),
			'phone' => Yii::t('base','telephone'),
			'email' =>Yii::t('base','email'),
			'agent_type'=>Yii::t('base','Agent Type'),
			'credit'=>Yii::t('base','Credit'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('agent_id',$this->agent_id);
		$criteria->compare('agent_number',$this->agent_number);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('agent_type',$this->agent_type,true);
		$criteria->compare('credit',$this->credit,true);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function judge($id){
		$rs=AgentCompany::model()->find('user_id='.$id);
		return $rs;
	}
	public function getAgentNumber($id){
		$user=User::model()->find('user_id='.$id);
		if($user->level==4){
			$sql="select * from zy_agent_company where user_id in (select user_id from zy_user where level=4)";
			$connection = Yii::app()->db;
			$command = $connection->createCommand($sql);
			$results = $command->queryAll();
			for($i=0;$i<count($results);$i++){
				$result[$i]=$results[$i]['agent_number'];
			}
			if($result==""){
				$agent_number=1001;
			}else{
				$num=count($result)-1;
				$agent_number=$result[$num]+1;
			}
		}else{
			$sql="select * from zy_agent_company where user_id in (select user_id from zy_user where level=7)";
			$connection = Yii::app()->db;
			$command = $connection->createCommand($sql);
			$results = $command->queryAll();
			for($i=0;$i<count($results);$i++){
				$result[$i]=$results[$i]['agent_number'];
			}
			if($result==""){
				$agent_number=2001;
			}else{
				$num=count($result)-1;
				$agent_number=$result[$num]+1;
			}
		}
		return $agent_number;
	}
	//显示代理人级别
	public function getagenttype($id){
		if($id==1){
			return '一级代理商';
		}elseif($id==2){
			return '二级代理商';
		}elseif($id==3){
			return  '三级代理商';
		}elseif($id==4){
			return '一级子代理';
		}elseif($id==5){
			return '二级子代理';
		}elseif($id==6){
			return '三级子代理';
		}
	}
	public function getAgentId($user_id){
		$user=User::model()->find('user_id='.$user_id);
		if($user->level==5){
			$agent=AgentPerson::model()->find('user_id='.$user_id);
		    $agent_id = $agent->agent_person_id;
		}else{
			$agent=AgentCompany::model()->find('user_id='.$user_id);
			$agent_id=$agent->agent_id;
		}
		return $agent_id;
	}
	public function getAgentName($user_id,$agent_id){
		$user=User::model()->find('user_id='.$user_id);
		if($user->level==5){
			$agent=AgentPerson::model()->find('agent_person_id='.$agent_id);
                }elseif($user->level==8){
                    $agent=  Custom::model()->find('custom_id='.$agent_id);
                    return $agent->custom_surname.$agent->custom_name;
                }else{
			$agent=AgentCompany::model()->find('agent_id='.$agent_id);
		}
		return $agent->name;
	}
}